package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditPageQueryResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

@Autowired
private WorkerCertificationAuditMapper workerCertificationAuditMapper;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }


    /**
     * 服务端提交认证申请
     *
     * @param dto
     */
    @Override
    public void workerSubmitAAuthentication(WorkerCertificationAuditAddReqDTO dto) {
        //1.判断dto是否为空
        if (BeanUtils.isEmpty(dto)){
            throw new BadRequestException("认证信息为空");
        }
        //2.或取当前的用户信息
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        String name = currentUserInfo.getName();

        WorkerCertification workerCertification = BeanUtils.copyBean(dto, WorkerCertification.class);
        workerCertification.setName(name);
        //workerCertification.setProviderId(currentUserInfo.getId());
        workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());

        WorkerCertificationAudit workerCertificationAudit = BeanUtils.copyBean(workerCertification,
                WorkerCertificationAudit.class);
        workerCertificationAudit.setServeProviderId(currentUserInfo.getId());
        workerCertificationAudit.setName(dto.getName());
        //向worker_certification_audit表中新增数据
        workerCertificationAuditMapper.insert(workerCertificationAudit);

        //3.向数据库中插入数据
        boolean result = this.save(workerCertification);
        if (!result){
            throw new DBException("提交认证信息失败");
        }
    }


    /**
     * 查询最新驳回原因
     * @return
     */
    @Override
    public Map<String,String> getRejectReason() {
        LambdaQueryWrapper<WorkerCertificationAudit> queryWrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .eq(WorkerCertificationAudit::getServeProviderId, UserContext.currentUser().getId())
                .eq(WorkerCertificationAudit::getCertificationStatus, CertificationStatusEnum.FAIL.getStatus())
                .orderByDesc(WorkerCertificationAudit::getAuditTime);
        List<WorkerCertificationAudit> workerCertificationAudits = workerCertificationAuditMapper.selectList(queryWrapper);
        Map<String,String> map=new HashMap<>();
        map.put("rejectReason",workerCertificationAudits.get(0).getRejectReason());
        return map;
    }

    /**
     * 审核服务人员认证分页查询
     * @param dto
     * @return
     */

}
